

package com.hazelcast.jet.impl.serialization;

import java.nio.ByteOrder;

import static com.hazelcast.internal.memory.GlobalMemoryAccessorRegistry.MEM;
import static com.hazelcast.internal.memory.GlobalMemoryAccessorRegistry.MEM_AVAILABLE;
import static com.hazelcast.internal.memory.HeapMemoryAccessor.ARRAY_BYTE_BASE_OFFSET;
import static com.hazelcast.internal.util.Preconditions.checkState;
import static java.nio.ByteOrder.nativeOrder;

public class UnsafeMemoryReader implements MemoryReader {

    private final boolean reverse;

    UnsafeMemoryReader(ByteOrder byteOrder) {
        checkState(MEM_AVAILABLE, "unsafe memory access is not available");
        this.reverse = nativeOrder() != byteOrder;
    }

    @Override
    public int readInt(byte[] bytes, int offset) {
        int value = MEM.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset);
        return reverse ? Integer.reverseBytes(value) : value;
    }

    @Override
    public long readLong(byte[] bytes, int offset) {
        long value = MEM.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset);
        return reverse ? Long.reverseBytes(value) : value;
    }
}
